clear

set memory 40000
set matsize 8000

use input_data/CFO_survey_data, clear

gen date_rd = mdy(month(date),1,year(date)) if date <. 
format date_rd  %tm

sort date_rd
merge m:1 date_rd using input_data/sp500_data
drop if _merge == 2

****** Options for figures *********
local polydegree 1
local scalefactor 2
local cicolor `""179 179 255""'
local polycolor `""0 0 255""'

************* Define variables *************
*** Support variables
gsort email yyyyqq
egen id = group(email) if email~=""
sort id yyyyqq
gen one = 1 + runiform(-0.0001,0.0001)

gen year_rd = year
replace year = year_rd + (qtr - 1)/4
drop year_rd

*** Had ID / No ID
gen hasid = .
replace hasid = 1 if email >""
gen noid = .
replace noid = 1 if email ==""
gen hadid_analysis = (hasid == 1) & yyyyqq >= 200502 & yyyyqq !=202001


*** Keep valid forecasts only
gen temp = 1 if opt_st_lb > -1 & opt_st_ub < 1
replace opt_st_ub = opt_st_ub * 100 if temp == 1
replace opt_st_lb = opt_st_lb * 100 if temp == 1
replace opt_st = opt_st * 100 if temp == 1
drop temp

gen temp = 1 if opt_lt_lb > -1 & opt_lt_ub < 1
replace opt_lt_ub = opt_lt_ub * 100 if temp == 1
replace opt_lt_lb = opt_lt_lb * 100 if temp == 1
replace opt_lt = opt_lt * 100 if temp == 1
drop temp

gen ci = opt_st_ub - opt_st_lb
gen ci_lt = opt_lt_ub - opt_lt_lb

drop if ci ==. 
drop if ci < 1 | ci > 200
drop if (opt_st_lb > opt_st | opt_st > opt_st_ub)

replace opt_lt_lb = . if ~(opt_lt_lb <= opt_lt & opt_lt <= opt_lt_ub)
replace opt_lt = . if opt_lt_lb == .
replace opt_lt_ub = . if opt_lt_lb == .

*** Remove repeating obs (more than one response per survey)
*** Keeping the CI that is closest to the previous one
gsort id yyyyqq
by id: gen d_ci_temp = abs(ci - ci[_n-1])
by id: replace d_ci_temp = abs(ci[_n+1] - ci) if d_ci_temp ==.

gsort id yyyyqq d_ci_temp
egen rank_d_ci_temp = rank(d_ci_temp), by(id yyyyqq)
drop if email ~="" & rank_d_ci_temp >1 & rank_d_ci_temp<.
drop d_ci_temp rank_d_ci_temp

*** Generate within-forecaster ranking and reverse ranking from last forecast 
gsort id yyyyqq
egen forecast_num = rank(yyyyqq) if hadid_analysis==1, by(id)
egen forecast_count_total = count(yyyyqq) if hadid_analysis==1, by(id)

*** Match to lagged forecasts: CI, hit/miss
gsort id yyyyqq

gen l4_opt_st = .
gen l4_ci = .
gen l4_fall_in_ci = .

by id: replace l4_opt_st = opt_st[_n-4] if id == id[_n-4] & yyyyqq - yyyyqq[_n-4] == 100
by id: replace l4_opt_st = opt_st[_n-3] if id == id[_n-3] & yyyyqq - yyyyqq[_n-3] == 100
by id: replace l4_opt_st = opt_st[_n-2] if id == id[_n-2] & yyyyqq - yyyyqq[_n-2] == 100
by id: replace l4_opt_st = opt_st[_n-1] if id == id[_n-1] & yyyyqq - yyyyqq[_n-1] == 100

by id: replace l4_ci = ci[_n-4] if id == id[_n-4] & yyyyqq - yyyyqq[_n-4] == 100
by id: replace l4_ci = ci[_n-3] if id == id[_n-3] & yyyyqq - yyyyqq[_n-3] == 100
by id: replace l4_ci = ci[_n-2] if id == id[_n-2] & yyyyqq - yyyyqq[_n-2] == 100
by id: replace l4_ci = ci[_n-1] if id == id[_n-1] & yyyyqq - yyyyqq[_n-1] == 100

by id: replace l4_fall_in_ci = fall_in_ci[_n-4] if id == id[_n-4] & yyyyqq - yyyyqq[_n-4] == 100 
by id: replace l4_fall_in_ci = fall_in_ci[_n-3] if id == id[_n-3] & yyyyqq - yyyyqq[_n-3] == 100 
by id: replace l4_fall_in_ci = fall_in_ci[_n-2] if id == id[_n-2] & yyyyqq - yyyyqq[_n-2] == 100 
by id: replace l4_fall_in_ci = fall_in_ci[_n-1] if id == id[_n-1] & yyyyqq - yyyyqq[_n-1] == 100 

gen d_ci = ci - l4_ci

***************************************************************
gen fall_in_ci100 = fall_in_ci * 100 // ORIGNAL fall_in_ci
gen fall_in_ci100_new = 0 if opt_st_lb ~= . & sp500_ret_1year < .
replace fall_in_ci100_new = 100 if opt_st_lb <= 100*sp500_ret_1year & 100*sp500_ret_1year <= opt_st_ub & sp500_ret_1year ~=.

gen fall_in_ci_lt100 = 0 if opt_lt_lb ~= . & sp500_ret_10year < .
replace fall_in_ci_lt100 = 100 if opt_lt_lb <= 100*sp500_ret_10year & 100*sp500_ret_10year <= opt_lt_ub & sp500_ret_10year ~=.

gen miss_ci = 1 if fall_in_ci == 0
replace miss_ci = 0 if fall_in_ci == 1
 
gen l4_miss_ci = 1 if l4_fall_in_ci == 0
replace l4_miss_ci = 0 if l4_fall_in_ci == 1
 
gen d_opt_st = opt_st - l4_opt_st

** CI matching
gen l4_ci1 = int(l4_ci)
gen l4_ci2 = int(l4_ci / 2) * 2
gen l4_ci5 = int(l4_ci / 5) * 5

gen yyyyqq_x_l4_ci = 100 * yyyyqq + l4_ci
gen yyyyqq_x_l4_ci1 = 100 * yyyyqq + l4_ci1
gen yyyyqq_x_l4_ci2 = 100 * yyyyqq + l4_ci2
gen yyyyqq_x_l4_ci5 = 100 * yyyyqq + l4_ci5

 
** Generate "random learning"
gen random_learning_ci = 0.7 * l4_ci + 0.3 * l4_ci * 0.2 * (2 * ((runiform() > 0.5) - 0.5))
gen random_learning_d_ci = random_learning_ci - l4_ci 


** Generate "forced learning"
gen forced_learning_ci = 0.7 * l4_ci - 0.1 * l4_ci * 2 * (l4_fall_in_ci - 0.5) ///
	+ 0.3 * l4_ci * (2 * ((runiform() > 0.5) - 0.5))
gen forced_learning_d_ci = forced_learning_ci - l4_ci


** "Last forecast" dummy
gsort id date
egen max_year = max(year) if hadid_analysis==1, by(id)
by id: gen last_forecast100 = 100 * (year > (max_year - 1)) if yyyyqq <= 202201 & forecast_count <. 

** S&P familiarity
gen sp_familiarity_dummy = 0 if sp_familiarity == 1 | sp_familiarity == 2
replace sp_familiarity_dummy = 1 if sp_familiarity == 3 | sp_familiarity == 4

egen sp_familiarity_dummy_all = mean(sp_familiarity_dummy) if hadid_analysis==1, by(id)

gen pos_forecast_er = min(forecast_er, 0) if forecast_er <.
gen neg_forecast_er = max(forecast_er, 0) if forecast_er <.
gen abs_forecast_er = abs(forecast_er) if forecast_er <.

*** Project characteristics
egen age_group_200801_all_temp = mean(age_group_200801) if id ~=., by(id)
egen age_201404_temp = mean(age_201404) if id ~=., by(id)
gen age = age_201404_temp + year - 2014.75  if id ~=.
replace age = age_group_200801_all_temp + year - 2008 if age ==.  & id ~=.
gen birth_year = year - age 
gen age_rd = round(age)
gen age_rd_decade = round(age, 10)
gen birth_year_rd = round(birth_year)
egen min_age = min(age) if hadid_analysis==1, by(id)
gen res_age = age - min_age
egen gender = median(gender_201404) if hadid_analysis==1, by(id)
gen log_age = ln(age)

areg size_revenue, absorb(id)
predict res_size_revenue, res 
replace res_size_revenue = . if res_size_revenue == 0

*** Mean-adjusted
egen mean_ci = mean(ci) if hadid_analysis == 1
reghdfe ci one if hadid_analysis == 1, absorb(yyyyqq id) resid
predict ci_temp, resid
gen ci_date_id_adj = ci_temp + mean_ci if hadid_analysis==1
drop ci_temp

reghdfe ci one if hadid_analysis == 1, absorb(yyyyqq) resid
predict ci_temp, resid 
gen ci_date_adj = ci_temp + mean_ci if hadid_analysis==1
drop ci_temp

egen mean_fall_in_ci100 = mean(fall_in_ci100) if hadid_analysis == 1
reghdfe fall_in_ci100 one if hadid_analysis == 1, absorb(yyyyqq id) resid
predict fall_in_ci100_temp, resid
gen fall_in_ci100_date_id_adj = fall_in_ci100_temp + mean_fall_in_ci100 if hadid_analysis==1
drop fall_in_ci100_temp

reghdfe fall_in_ci100 one if hadid_analysis == 1, absorb(yyyyqq) resid
predict fall_in_ci100_temp, resid 
gen fall_in_ci100_date_adj = fall_in_ci100_temp + mean_fall_in_ci100 if hadid_analysis==1
drop fall_in_ci100_temp

gen logforecast_num = ln(forecast_num)/ln(10) if hadid_analysis == 1
gen ci_first = ci if forecast_num==1 & hadid_analysis == 1
gen fall_in_ci100_first = fall_in_ci100 if forecast_num==1 & hadid_analysis == 1

egen median_opt_st = median(opt_st), by(yyyyqq) 

**************************
** Table 1
**************************
tabstat opt_st_ub opt_st opt_st_lb ci d_ci fall_in_ci miss_ci  ///
	forecast_num logforecast_num ///
	birth_year_rd age size_revenue sp_familiarity_dummy_all, ///
	stats(n mean sd min p10 p25 p50 p75 p90 max)

**************************
** Table 3
**************************
preserve
collapse (mean) year = year ///
	(mean) mean_ci = ci ///
	(count) num_ci = ci ///
	(mean) mean_fall_in_ci100 = fall_in_ci100 ///
	(count) num_fall_in_ci100 = fall_in_ci100 ///
	(mean) mean_ci_first = ci_first ///
	(count) num_ci_first = ci_first ///
	(mean) mean_fall_in_ci100_first = fall_in_ci100_first ///
	(count) num_fall_in_ci100_first = fall_in_ci100_first, by(yyyyqq)
tsset yyyyqq

newey2  mean_ci year [aw=num_ci], lag(4) force
reg mean_ci year [aw=num_ci]

newey2 mean_ci_first year [aw=num_ci_first], lag(4) force
reg mean_ci_first year [aw=num_ci_first]

newey2 mean_fall_in_ci100 year [aw=num_fall_in_ci100], lag(4) force
reg mean_fall_in_ci100 year [aw=num_fall_in_ci100]

newey2 mean_fall_in_ci100_first year [aw=num_fall_in_ci100_first], lag(4) force
reg mean_fall_in_ci100_first year [aw=num_fall_in_ci100_first]

restore

**************************
** Table 4
**************************
reghdfe ci one if hadid_analysis==1, absorb(yyyyqq) cluster(id yyyyqq)
outreg2 using tables/table4, excel replace bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe ci one if hadid_analysis==1, absorb(id) cluster(id yyyyqq)
outreg2 using tables/table4, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe ci one if hadid_analysis==1, absorb(yyyyqq id) cluster(id yyyyqq)
outreg2 using tables/table4, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe ci forecast_num if hadid_analysis==1, absorb(yyyyqq id) cluster(id yyyyqq)
outreg2 using tables/table4, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe ci logforecast_num if hadid_analysis==1, absorb(yyyyqq id) cluster(id yyyyqq)
outreg2 using tables/table4, excel append bdec(3) tdec(3) asterisk(coef) adjr2


reghdfe fall_in_ci100 one if hadid_analysis==1, absorb(yyyyqq) cluster(id yyyyqq)
outreg2 using tables/table4, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe fall_in_ci100 one if hadid_analysis==1, absorb(id) cluster(id yyyyqq)
outreg2 using tables/table4, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe fall_in_ci100 one if hadid_analysis==1, absorb(yyyyqq id) cluster(id yyyyqq)
outreg2 using tables/table4, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe fall_in_ci100 forecast_num if hadid_analysis==1, absorb(yyyyqq id) cluster(id yyyyqq)
outreg2 using tables/table4, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe fall_in_ci100 logforecast_num if hadid_analysis==1, absorb(yyyyqq id) cluster(id yyyyqq)
outreg2 using tables/table4, excel append bdec(3) tdec(3) asterisk(coef) adjr2

**************************
** Table 5
**************************
reghdfe d_ci l4_miss_ci if hadid_analysis==1, absorb(yyyyqq) cluster(yyyyqq)
outreg2 using tables/table5, excel replace bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe d_ci l4_miss_ci if hadid_analysis==1, absorb(yyyyqq id) cluster(yyyyqq)
outreg2 using tables/table5, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe d_ci l4_miss_ci l4_ci if hadid_analysis==1, absorb(yyyyqq) cluster(yyyyqq)
outreg2 using tables/table5, excel append bdec(3) tdec(3) asterisk(coef) adjr2


reghdfe d_ci l4_miss_ci if hadid_analysis==1, absorb(yyyyqq_x_l4_ci) cluster(yyyyqq)
outreg2 using tables/table5, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe d_ci l4_miss_ci if hadid_analysis==1, absorb(yyyyqq_x_l4_ci1) cluster(yyyyqq)
outreg2 using tables/table5, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe d_ci l4_miss_ci if hadid_analysis==1, absorb(yyyyqq_x_l4_ci2) cluster(yyyyqq)
outreg2 using tables/table5, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe d_ci l4_miss_ci if hadid_analysis==1, absorb(yyyyqq_x_l4_ci5) cluster(yyyyqq)
outreg2 using tables/table5, excel append bdec(3) tdec(3) asterisk(coef) adjr2

**************************
** Table 6
**************************
reghdfe ci_date_adj one if hadid_analysis==1 & age <., absorb(id) cluster(id)
outreg2 using tables/table6, excel replace bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe ci_date_adj age if hadid_analysis==1, cluster(yyyyqq id)
outreg2 using tables/table6, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe ci_date_adj log_age if hadid_analysis==1, cluster(yyyyqq id)
outreg2 using tables/table6, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe ci_date_adj birth_year if hadid_analysis==1, cluster(yyyyqq id)
outreg2 using tables/table6, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe ci_date_adj age birth_year if hadid_analysis==1, cluster(yyyyqq id)
outreg2 using tables/table6, excel append bdec(3) tdec(3) asterisk(coef) adjr2

	
**************************
** Table 7
**************************
reghdfe ci_date_adj one if hadid_analysis==1 & size_revenue <., absorb(id) cluster(yyyyqq id)
outreg2 using tables/table7, excel replace bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe ci_date_adj size_revenue if hadid_analysis==1 & size_revenue <., noabsorb cluster(yyyyqq id)
outreg2 using tables/table7, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe ci_date_adj size_revenue if hadid_analysis==1 & size_revenue <., absorb(id) cluster(yyyyqq id)
outreg2 using tables/table7, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe ci_date_adj one if hadid_analysis==1 & size_employees <., absorb(id) cluster(yyyyqq id)
outreg2 using tables/table7, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe ci_date_adj size_employees if hadid_analysis==1 & size_employees <., noabsorb cluster(yyyyqq id)
outreg2 using tables/table7, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe ci_date_adj size_employees if hadid_analysis==1 & size_employees <., absorb(id) cluster(yyyyqq id)
outreg2 using tables/table7, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe ci_date_adj i.industry if hadid_analysis==1 & industry <., noabsorb cluster(yyyyqq id)
outreg2 using tables/table7, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe ci_date_adj i.industry  if hadid_analysis==1 & industry <., absorb(id) cluster(yyyyqq id)
outreg2 using tables/table7, excel append bdec(3) tdec(3) asterisk(coef) adjr2

************************************ 
************* Appendix *************
************************************

*** Appendix Table A.1
tab industry
tab industry if ownership == 1 | ownership == 2
tab industry if ownership == 3 | ownership == 4 | ownership == 5

tab size_revenue
tab size_revenue if ownership == 1 | ownership == 2
tab size_revenue if ownership == 3 | ownership == 4 | ownership == 5

tab size_employees
tab size_employees if ownership == 1 | ownership == 2
tab size_employees if ownership == 3 | ownership == 4 | ownership == 5

tab age_rd_decade
tab age_rd_decade if ownership == 1 | ownership == 2
tab age_rd_decade if ownership == 3 | ownership == 4 | ownership == 5

tab gender if gender == 1 | gender == 2  | gender == 3 
tab gender if (gender == 1 | gender == 2 | gender == 3 ) & (ownership == 1 | ownership == 2)
tab gender if (gender == 1 | gender == 2 | gender == 3 ) & (ownership == 3 | ownership == 4 | ownership == 5) 

*** Appendix Table C.1
reghdfe last_forecast100 miss_ci if hadid_analysis==1, absorb(yyyyqq_x_l4_ci) cluster(id yyyyqq)
outreg2 using tables/table_appC, excel replace bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe last_forecast100 miss_ci if hadid_analysis==1, absorb(yyyyqq_x_l4_ci1) cluster(id yyyyqq)
outreg2 using tables/table_appC, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe last_forecast100 miss_ci if hadid_analysis==1, absorb(yyyyqq_x_l4_ci2) cluster(id yyyyqq)
outreg2 using tables/table_appC, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe last_forecast100 miss_ci if hadid_analysis==1, absorb(yyyyqq_x_l4_ci5) cluster(id yyyyqq)
outreg2 using tables/table_appC, excel append bdec(3) tdec(3) asterisk(coef) adjr2

*** Appendix Table D.1, Panel A
reghdfe random_learning_d_ci l4_miss_ci if hadid_analysis==1, absorb(yyyyqq) cluster(yyyyqq)
outreg2 using tables/table_appD_A, excel replace bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe random_learning_d_ci l4_miss_ci if hadid_analysis==1, absorb(yyyyqq id) cluster(yyyyqq)
outreg2 using tables/table_appD_A, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe random_learning_d_ci l4_miss_ci l4_ci if hadid_analysis==1, absorb(yyyyqq) cluster(yyyyqq)
outreg2 using tables/table_appD_A, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe random_learning_d_ci l4_miss_ci if hadid_analysis==1, absorb(yyyyqq_x_l4_ci) cluster(yyyyqq)
outreg2 using tables/table_appD_A, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe random_learning_d_ci l4_miss_ci if hadid_analysis==1, absorb(yyyyqq_x_l4_ci1) cluster(yyyyqq)
outreg2 using tables/table_appD_A, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe random_learning_d_ci l4_miss_ci if hadid_analysis==1, absorb(yyyyqq_x_l4_ci2) cluster(yyyyqq)
outreg2 using tables/table_appD_A, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe random_learning_d_ci l4_miss_ci if hadid_analysis==1, absorb(yyyyqq_x_l4_ci5) cluster(yyyyqq)
outreg2 using tables/table_appD_A, excel append bdec(3) tdec(3) asterisk(coef) adjr2

*** Appendix Table D.1, Panel B
reghdfe forced_learning_d_ci l4_miss_ci if hadid_analysis==1, absorb(yyyyqq) cluster(yyyyqq)
outreg2 using tables/table_appD_B, excel replace bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe forced_learning_d_ci l4_miss_ci if hadid_analysis==1, absorb(yyyyqq id) cluster(yyyyqq)
outreg2 using tables/table_appD_B, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe forced_learning_d_ci l4_miss_ci l4_ci if hadid_analysis==1, absorb(yyyyqq) cluster(yyyyqq)
outreg2 using tables/table_appD_B, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe forced_learning_d_ci l4_miss_ci if hadid_analysis==1, absorb(yyyyqq_x_l4_ci) cluster(yyyyqq)
outreg2 using tables/table_appD_B, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe forced_learning_d_ci l4_miss_ci if hadid_analysis==1, absorb(yyyyqq_x_l4_ci1) cluster(yyyyqq)
outreg2 using tables/table_appD_B, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe forced_learning_d_ci l4_miss_ci if hadid_analysis==1, absorb(yyyyqq_x_l4_ci2) cluster(yyyyqq)
outreg2 using tables/table_appD_B, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe forced_learning_d_ci l4_miss_ci if hadid_analysis==1, absorb(yyyyqq_x_l4_ci5) cluster(yyyyqq)
outreg2 using tables/table_appD_B, excel append bdec(3) tdec(3) asterisk(coef) adjr2

*** Appendix Table E.1, Panel A
reghdfe d_ci l4_miss_ci if abs(opt_st - median_opt_st)<=1 & hadid_analysis==1, absorb(yyyyqq_x_l4_ci) cluster(yyyyqq)
outreg2 using tables/table_appE, excel replace bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe d_ci l4_miss_ci if abs(opt_st - median_opt_st)<=3 & hadid_analysis==1, absorb(yyyyqq_x_l4_ci) cluster(yyyyqq)
outreg2 using tables/table_appE, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe d_ci l4_miss_ci if abs(opt_st - median_opt_st)<=5 & hadid_analysis==1, absorb(yyyyqq_x_l4_ci) cluster(yyyyqq)
outreg2 using tables/table_appE, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe d_ci l4_miss_ci if abs(opt_st - median_opt_st)<=10 & hadid_analysis==1, absorb(yyyyqq_x_l4_ci) cluster(yyyyqq)
outreg2 using tables/table_appE, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe d_ci l4_miss_ci if abs(opt_st - median_opt_st)<=1 & hadid_analysis==1, absorb(yyyyqq_x_l4_ci2) cluster(yyyyqq)
outreg2 using tables/table_appE, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe d_ci l4_miss_ci if abs(opt_st - median_opt_st)<=3 & hadid_analysis==1, absorb(yyyyqq_x_l4_ci2) cluster(yyyyqq)
outreg2 using tables/table_appE, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe d_ci l4_miss_ci if abs(opt_st - median_opt_st)<=5 & hadid_analysis==1, absorb(yyyyqq_x_l4_ci2) cluster(yyyyqq)
outreg2 using tables/table_appE, excel append bdec(3) tdec(3) asterisk(coef) adjr2

reghdfe d_ci l4_miss_ci if abs(opt_st - median_opt_st)<=10 & hadid_analysis==1, absorb(yyyyqq_x_l4_ci2) cluster(yyyyqq)
outreg2 using tables/table_appE, excel append bdec(3) tdec(3) asterisk(coef) adjr2

*** Update of BGH (2013)
tabstat opt_st_ub opt_st opt_st_lb, by(date) stats(mean)
tabstat fall_in_ci100, by(date) stats(mean)


**************************
** Figure 1, Panels A and B
**************************
tabstat hasid noid, by(year) stat(n)
tabstat forecast_count if forecast_num==1, by(forecast_count) stat(n)

**************************
** Figure 3, Panels A and B
**************************
tabstat ci, by(year) stats(n p10 p25 p50 p75 p90)
tabstat fall_in_ci100 , by(year) stats(mean)

**************************
** Figure 4, Panel A
**************************
binsreg ci_date_id_adj forecast_num if hadid_analysis==1 & inrange(forecast_num,1,40), ///
	dotsplotopt(mcolor(`cicolor')) ///
	ci(1 1) ciplotopt(lcolor(`cicolor')) ///
	randcut(1) masspoints(veryfew) ///	
	reghdfeopt(cluster(id)) vce(cluster id) ///
	polyreg(`polydegree') polyregplotopt(lcolor(`polycolor') lwidth(thick)) ///
	xtitle("Forecast number") ytitle("Confidence interval" "(Adjusted; %)") ///
    ylabel(5(5)20) yscale(range(5 20)) ///
	xlabel(0(10)40) xscale(range(0 40)) ///
	name(fig4a, replace) scale(`scalefactor')

graph export figures/fig4A_within_CFO_w_FE_ci_width.pdf, replace
	
**************************
** Figure 4, Panel B
**************************
binsreg fall_in_ci100 forecast_num if hadid_analysis==1 & inrange(forecast_num,1,40), ///
	dotsplotopt(mcolor(`cicolor')) ///
	ci(1 1) ciplotopt(lcolor(`cicolor')) ///
	randcut(1) masspoints(veryfew) ///	
	reghdfeopt(cluster(id)) vce(cluster id) /// 
	polyreg(`polydegree') polyregplotopt(lcolor(`polycolor') lwidth(thick)) ///
	xtitle("Forecast number") ytitle("Hit rate (Adjusted; %)")  ///
	xlabel(0(10)40) xscale(range(0 40)) ///
	 name(fig4b, replace) scale(`scalefactor')
	
graph export figures/fig4B_within_CFO_w_FE_hit_rate.pdf, replace

**************************
** Figure 5, Panel A
**************************
binsreg ci_date_adj age ///
	if hadid_analysis==1 & inrange(age,30,80), ///
	dotsplotopt(mcolor(`cicolor')) ///
	ci(1 1) at(mean) ciplotopt(lcolor(`cicolor')) ///
	vce(cluster id) /// 
	polyreg(`polydegree') polyregplotopt(lcolor(`polycolor') lwidth(thick)) ///
	xtitle("Age (years)") ytitle("Confidence interval" "(Adjusted; %)")  ///
    ylabel(5(5)25) yscale(range(5 25)) ///
	xlabel(30(10)80) xscale(range(30 80)) ///
	name(fig5a, replace) scale(`scalefactor')
	
graph export figures/fig5A_CFO_age.pdf, replace
	
**************************
** Figure 5, Panel B
**************************	
binsreg ci_date_adj birth_year ///
	if hadid_analysis==1, ///
	dotsplotopt(mcolor(`cicolor')) ///
	ci(1 1) at(mean) ciplotopt(lcolor(`cicolor')) ///
	reghdfeopt(cluster(id)) vce(cluster id) /// 
	polyreg(`polydegree') polyregplotopt(lcolor(`polycolor') lwidth(thick)) ///
	xtitle("Birth year") ytitle("Confidence interval" "(Adjusted; %)") ///
    ylabel(5(5)25) yscale(range(5 25)) ///
	xlabel(1930(10)1985) xscale(range(1930 1985)) ///
	name(fig5c, replace) scale(`scalefactor')
	
graph export figures/fig5B_CFO_birth_year.pdf, replace

**************************
** Figure 6, Panel A
**************************
binsreg ci_date_adj size_revenue ///
	if hadid_analysis==1, ///
	dotsplotopt(mcolor(`cicolor')) ///
	ci(1 1) ciplotopt(lcolor(`cicolor')) ///
	absorb(id) vce(cluster id) /// 
	polyreg(`polydegree') polyregplotopt(lcolor(`polycolor') lwidth(thick)) ///
	xtitle("Revenue bin (1:7)") ytitle("Confidence interval" "(Adjusted; %)") ///
	xlabel(1(1)7) ///
	ylabel(12 "12" 14 "14" 16 "16" 18 "18") yscale(range(12 18)) ///
	name(fig6b, replace) scale(`scalefactor')

graph export figures/fig6A_CFO_firm_size_revenue_FE.pdf, replace

**************************
** Figure 6, Panel B
**************************
binsreg ci_date_adj res_size_revenue ///
	if hadid_analysis==1 & !missing(res_size_revenue) & inrange(res_size_revenue,-2,2),  ///
	dotsplotopt(mcolor(`cicolor')) ///
	ci(1 1) ciplotopt(lcolor(`cicolor')) ///
	absorb(id) vce(cluster id) /// 
	polyreg(`polydegree') polyregplotopt(lcolor(`polycolor') lwidth(thick)) ///
	xtitle("Change in Revenue bin") ytitle("Confidence interval" "(Adjusted; %)") ///
	xlabel(-2(1)2) xscale(range(-2 2)) ///
	ylabel(12 "12" 14 "14" 16 "16" 18 "18") yscale(range(12 18)) ///
	name(fig6b_res, replace) scale(`scalefactor') 

graph export figures/fig6B_CFO_firm_res_size_revenue_FE.pdf, replace

**************************
** Figure 7
**************************
binsreg sp_familiarity_dummy_all ci_date_adj ///
	if hadid_analysis==1 & inrange(ci_date_adj,0,40), ///
	dotsplotopt(mcolor(`cicolor')) ///
	ci(1 1) at(mean) ciplotopt(lcolor(`cicolor')) ///
	reghdfeopt(cluster(id)) vce(cluster id) /// 
	polyreg(2) polyregplotopt(lcolor(`polycolor') lwidth(thick)) ///
	ytitle("Stock market" "familiarity (0/1)") xtitle("Confidence interval (Adjusted; %)") ///
	ylabel(0.65 "0.65" 0.7 "0.70" 0.75 "0.75" 0.8 "0.80" 0.85 "0.85" 0.9 "0.90" 0.95 "0.95") ///
	name(fig7_quadratic, replace) scale(1.5) 
	
graph export figures/fig7_CFO_sp_familiarity_quadratictrend.pdf, replace
